/**********************************************************************
 * SPRITE class
 * by Marc-Henri Vuillaume
 **********************************************************************
 * * * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 **********************************************************************/
package mhv.fr.sprite;

import java.awt.Graphics2D;
import java.awt.Rectangle;
import mhv.fr.sprite.animator.SpriteAnimator;
import mhv.fr.sprite.controller.SpriteController;

/**
 * Simple sprite class using bitmap sprite.
 * @author Marc-Henri
 */
public class Sprite extends Sprite2D {

    /** 
     * The image container 
     */
    private ImageEntity imageEntity;
    private SpriteAnimator animator;
    private SpriteController controller;

    public ImageEntity getImageEntity() {
        return imageEntity;
    }

    public void setImageEntity(ImageEntity imageEntity) {
        this.imageEntity = imageEntity;
    }

    public SpriteController getController() {
        return controller;
    }

    public void setController(SpriteController controller) {
        this.controller = controller;
        this.controller.setGameEntity(gameEntity);
    }

    /**
     * Get the bouning box of this Sprite
     * @return Rectangle
     */
    @Override
    public Rectangle getBounds() {
        Rectangle r;
        r = new Rectangle((int) getX() - imageEntity.width() / 2,
                (int) getY() - imageEntity.height() / 2,
                imageEntity.width(), imageEntity.height());
        return r;
    }

    /**
     * Loag and store the sprite image.
     * @param filename
     */
    public void loadImage(String filename) {
        imageEntity.load(filename);
    }

    public SpriteAnimator getAnimator() {
        return animator;
    }

    public void setAnimator(SpriteAnimator animator) {
        this.animator = animator;
        if (animator != null) {
            imageEntity.setImage(animator.getCurrentImage());
        }
    }

    public boolean isAnimate() {
        return animator != null;
    }

    public void update() {
        if (isAnimate()) {
            animator.updateAnimation();
        }
        controller.move();
    }

    @Override
    public void setAlive(boolean alive) {
        if (!isAlive() && alive) {
            controller.init();
        }
        super.setAlive(alive);
    }

    /**
     * draw this Sprite into the drawng surface
     */
    @Override
    public void draw() {
        if (isAnimate()) {
            imageEntity.setImage(animator.getCurrentImage());
        }
        transform();
        imageEntity.draw(getGraphics());
    }

    /**
     * Construct and initialise the Sprite object.
     * @param g2d Graphics2D : the drawing surface
     */
    public Sprite(Graphics2D g2d) {
        super(g2d);
        animator = null;
        imageEntity = new ImageEntity();
        controller = new SpriteController(gameEntity);
    }
}
